package com.dsj.gxrd.modules.activity.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsj.gxrd.modules.activity.domain.entity.RdNews;
import com.dsj.gxrd.modules.activity.domain.query.RdNewsQuery;
import com.dsj.gxrd.modules.activity.domain.vo.RdActivityTendencyVo;
import com.dsj.gxrd.modules.activity.domain.vo.RdFiveActivityVo;
import com.dsj.gxrd.modules.activity.domain.vo.RdNewsRankVo;
import com.dsj.gxrd.modules.activity.domain.vo.RdNewsVo;
import com.dsj.gxrd.modules.activity.mapper.RdNewsMapper;
import com.dsj.gxrd.modules.activity.service.RdNewsService;
import com.dsj.gxrd.modules.book.domain.dto.RdFileDto;
import com.dsj.gxrd.modules.book.enums.FileRelationTypeEnum;
import com.dsj.gxrd.modules.book.service.IRdFileService;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


/**
 * @author songpin566
 * @date 2023/10/07
 **/
@Service
public class RdNewsServiceImpl extends ServiceImpl<RdNewsMapper, RdNews> implements RdNewsService {

    @Autowired
    private IRdFileService rdFileService;

    @Override
    public List<RdNewsVo> selectRdNewsList(RdNewsQuery rdNewsQuery) {
        List<RdNewsVo> list;
        String isGxrdcwh = rdNewsQuery.getIsGxrdcwh();
        if ("Y".equalsIgnoreCase(isGxrdcwh)) {
            list = this.baseMapper.selectRdcwh(rdNewsQuery);
        } else {
            list = this.baseMapper.selectRdNewsList(rdNewsQuery);
        }

        for (RdNewsVo rdNewsVo : list) {
            List<RdFileDto> rdFile = rdFileService.listFiles(rdNewsVo.getRdNewsId(), FileRelationTypeEnum.NEWS_IMAGE);
            rdNewsVo.setFileList(rdFile);
        }
        return list;
    }

    @Override
    public RdNewsVo getInfo(Long rdNewsId) {
        RdNews rdNews = getById(rdNewsId);
        if(rdNews==null){
            throw new ServiceException("新闻id有误!");
        }
        RdNewsVo rdNewsVo = BeanUtils.copyProperties(rdNews, RdNewsVo.class);
        List<RdFileDto> rdFiles = rdFileService.listFiles(rdNewsId, FileRelationTypeEnum.NEWS_IMAGE);
        rdNewsVo.setFileList(rdFiles);
        return rdNewsVo;
    }

    @Override
    public List<RdNewsVo> selectSummaryList(Long deptId) {
        return this.baseMapper.selectSummaryList(deptId);
    }

    @Override
    public List<RdNewsRankVo> activityRank() {
        return this.baseMapper.activityRank();
    }

    @Override
    public Map<Integer, List<RdActivityTendencyVo>> activityTendency() {
        List<RdActivityTendencyVo> list = this.baseMapper.activityTendency();
        return list.stream().collect(Collectors.groupingBy(RdActivityTendencyVo::getYear));
    }

    @Override
    public List<RdFiveActivityVo> fiveActivity() {
        return this.baseMapper.fiveActivity();
    }

    @Override
    public List<RdNewsVo> getImageFiveList() {
        return this.baseMapper.getImageFiveList();
    }

    @Override
    public List<RdNewsVo> getNewestHeadNews() {
        return this.baseMapper.getNewestHeadNews();
    }

}
